using HCore.Tests.Runtime.ShootingEditor2D.Scripts.Command;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.Support;
using HCore.Unity.Core;
using UnityEngine;

namespace HCore.Tests.Runtime.ShootingEditor2D.Scripts.ViewController.GamePlay
{
    public class Bullet : HCoreCommandMonoBehaviour
    {
        private Rigidbody2D mRigidbody2D;

        protected override void Awake()
        {
            base.Awake();
            mRigidbody2D = GetComponent<Rigidbody2D>();
            Destroy(gameObject, 5); // 超出5秒，自动销毁
        }

        private void Start()
        {
            var playerComponent = GameUtils.MainPlayerGameObject.GetComponent<Player>();
            // 给子弹一个初始速度
            mRigidbody2D.linearVelocity = Vector2.right * 10 * playerComponent.IsRight();
        }

        private void OnCollisionEnter2D(Collision2D other)
        {
            if (other.gameObject.CompareTag("Enemy"))
            {
                SendCommand(new KilledEnemyCommand());
                Destroy(other.gameObject);
                Destroy(gameObject);
            }
        }
    }
}